package com.cow.sram.module.managerResource;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cow.framework.core.exception.BusinessException;
import com.cow.framework.utils.reflection.MapToBeanUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.comm.config.constant.MessageConstants;
import com.cow.sram.comm.config.tree.TreeDefService;
import com.cow.sram.data.entity.Resourceinfo;
import com.cow.sram.module.distribution.RoleResourceService;
import com.github.pagehelper.PageInfo;
/**
 * 普通资源管理
 * @author Administrator
 *
 */
@RestController
@EnableAutoConfiguration
@Transactional
@RequestMapping(value="/managerResource")
public class ResourceinfoController extends BaseComponent{

	@Autowired
	ResourceinfoService resourceInfoService;
	@Autowired
	private TreeDefService treeDefService;
	@Autowired
	RoleResourceService roleResourceService;
	
	/**查询资源列表*/
	@RequestMapping(value="/findResourceInfo")
	public Object findResourceInfo(@RequestBody Map parmMap){
		//普通资源与树形资源共用
		List<Map> resourceList = resourceInfoService.findResourceInfo(parmMap);	
		//判断是否是树形
		if(parmMap.get("resourcetype").toString().startsWith("t")){
			resourceList = resourceInfoService.mapListToTree(resourceList);
		}
		return new PageInfo<Map>(resourceList);
	}
	
	/**树形资源*/
	@RequestMapping(value="/findTreeResourceInfo")
	public Object findTreeResourceInfo(@RequestBody Map parmMap){
		//普通资源与树形资源共用
		List<Map> resourceList = resourceInfoService.findResourceInfo(parmMap);	
		//判断是否是树形
		if(parmMap.get("resourcetype").toString().startsWith("t")){
			resourceList = resourceInfoService.mapListToTree(resourceList);
		}
		return resourceList;
		
	}
	
	/**根据ID查看资源*/
	@RequestMapping(value="/getResourceInfoById")
	public Object getResourceInfoById(@RequestParam Integer id){
		return resourceInfoService.getResourceInfoById(id);
	}
	
	/**新增记录*/
	@RequestMapping(value="/saveResourceInfo")
	public Object saveResourceInfo(@RequestBody Resourceinfo rsc){
		rsc.setValidFlag(1);
		rsc.setHasChild(0);
		rsc.setOrderNo(1);
		 resourceInfoService.saveResourceInfo(rsc);
		//更新缓存
		 treeDefService.addTreeNodeCache(rsc);
		return rsc.getResourceid();
	
	}
	
	/**修改*/
	@RequestMapping(value="/editResourceInfo")
	public Object editResourceInfo(@RequestBody Map parmMap){
		//根据Id查询资源
		Resourceinfo rsc=resourceInfoService.getResourceinfoID(Integer.valueOf(parmMap.get("resourceid").toString()));
		MapToBeanUtil.copyMapToBean(parmMap, rsc);  //拷贝到实体
		resourceInfoService.saveResourceInfo(rsc);
		//更新缓存
		treeDefService.updateTreeNodeCache(rsc);
		return "success";
		
	}
	
	/**删除资源*/
	@RequestMapping(value="/delResourceInfo")
	public Object delResourceInfo(@RequestParam Integer id){
		Resourceinfo rsc =resourceInfoService.getResourceinfoID(id);
		//判断是否是树形
		if(rsc.getResourcetype().startsWith("t")){
			if(redisUtil.get("treeDef-" + rsc.getResourcetype() +"-"+id)==null){
				//更新缓存
				treeDefService.delTreeNodeCache(rsc);			
			}else{
				throw new BusinessException("B1015",MessageConstants.B1015);
			}
		}
		roleResourceService.delByResourceId(id);
		resourceInfoService.delResourceInfo(id);
		return "success";
	}
	
}
